package com.publicize.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.publicize.commom.commonutils.Result;
import com.publicize.commom.servicebase.exception.MyException;
import com.publicize.entity.User;
import com.publicize.entity.Vo.UserSelectVo;
import com.publicize.mapper.UserMapper;
import com.publicize.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author bs
 * @since 2021-03-04
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public List<User> selectUserByCondition(String startTime, String endTime, String username) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        if(startTime!=null) queryWrapper.ge("create_time",startTime);
        if(endTime!=null) queryWrapper.le("create_time",endTime);
        if(username!=null) queryWrapper.le("username",username);
        List<User> users = baseMapper.selectList(queryWrapper);
        return users;
    }
    //删除用户
    @Override
    public void removeUser(String id) {
        int i = baseMapper.deleteById(id);
        if(i!=1) throw new MyException("删除失败",201);
    }
    //根据id将管理员添加/移除黑名单
    @Override
    public Integer BlacklistUser(String id) {
        User user = baseMapper.selectById(id);
        if(user.getIsBlacklist()==1){
            user.setIsBlacklist(0);
        } else {
            user.setIsBlacklist(1);
        }
        int i = baseMapper.updateById(user);
        if(i!=1) throw new MyException("操作失败",201);
        return user.getIsBlacklist();
    }
    //根据已封装条件查询
    @Override
    public Result selectUserByCondition(UserSelectVo userSelectVo) {
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        List<User> userList=new ArrayList<>();
        //分页操作
        Page<User> page=new Page<>(userSelectVo.getPage(),userSelectVo.getLimit());
        //加入条件查询
        if(!StringUtils.isEmpty(userSelectVo.getKey())){
            queryWrapper.like("username",userSelectVo.getKey());
            queryWrapper.or();
            queryWrapper.eq("id",userSelectVo.getKey());
        }
        //根据排序方式
        if(userSelectVo.getMark()==0){
            //根据注册时间排序
            queryWrapper.orderByDesc("create_time");
        }else if(userSelectVo.getMark()==1){
            //根据发布文章数量排序
            queryWrapper.orderByDesc("user_essay_number");

        }else {
            //根据平均成绩查询
            queryWrapper.orderByDesc("user_average_score");
        }
        IPage<User> userIPage = baseMapper.selectPage(page, queryWrapper);
        //得到总数据
        userList = userIPage.getRecords();
        long total = userIPage.getTotal();
        return Result.ok().data("userList",userList).data("total",total);
    }

}
